This demonstration will show you how to make interactive bar graphs using Plotly. Below we load all of the needed libraries.

Plotly is the library for makint the graphs. Ecdat is were the data will come from. dplyr and forcats are used for data manipulation.

In the code below we are going to make a table from which we will derive are bar graphs. The data we are using is the PSID dataset which provides some information on such variables as marital status, education, age, and other data.

#bar graphs
married_table <- PSID %>%
        count(married)

married_table
ABCDEFGHIJ0123456789
married
<fct>
n
<int>
married3071
never married681
widowed90
divorced645
separated317
NA/DF9
no histories43

Next, we will make are first bar graph. It is a simple graph that shows the marital status of the respondents.

# Create a bar chart of marriage
married_table %>%
        plot_ly(x=~married, y= ~n) %>%
        add_bars()
marriednever marriedwidoweddivorcedseparatedNA/DFno histories050010001500200025003000
marriedn

In the graph below, we will rearrange the order of the bars so they are shown for largest to smallest. This is often much more visually appealing for the audience. To do this we will use the fct_reorder function from the forcats library. Below is the code and the output.

## Reorder bars
married_table %>%
        mutate(married = fct_reorder(married, 
                                     n, .desc = T)) %>% #fct_reorder from forcats
        plot_ly(x = ~married, y = ~n) %>% 
        add_bars() 
marriednever marrieddivorcedseparatedwidowedno historiesNA/DF050010001500200025003000
marriedn

The code below is mostly the same. The only difference is that we are adding a label for the x and y axis. This is done by adding the layout function and including the needed information.

#Add axes titles
married_table %>%
        mutate(married = fct_reorder(married, 
                                     n, .desc = T)) %>% #fct_reorder from forcats
        plot_ly(x = ~married, y = ~n) %>% 
        add_bars() %>% 
        layout(xaxis = list(title= "Marital Status",
                            showgrid=T),
               yaxis = list(title="Count"))
marriednever marrieddivorcedseparatedwidowedno historiesNA/DF050010001500200025003000
Marital StatusCount

The code below shows how to create hover info. What this means is that when the most hovers over a bar in the bar graph it will display information that we want. To do this you have to add the hoverinfo argument to the plot_ly function as shown below.

#hoverinfo
PSID %>%
        count(married) %>%
        plot_ly(x=~married, y=~n, hoverinfo="y") %>%
        add_bars()
marriednever marriedwidoweddivorcedseparatedNA/DFno histories050010001500200025003000
marriedn

THe final shows ow to make stacked bar graph. By stacking the bars we are able to include an additional variable in the graph. For our purpose we will display a count of sex and major together. To this you must specify the color in the plot_ly function and the type of barmode in the layout function.

#stackbar
Mathlevel %>%
        count(sex, major) %>%
        plot_ly(x = ~sex, y = ~n, color = ~major) %>%
        add_bars() %>%
        layout(barmode = 'stack')